package com.hxc.interest.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.hxc.basicdata.domain.TCommunityInfo;
import com.hxc.basicdata.mapper.TCommunityInfoMapper;
import com.hxc.common.core.domain.AjaxResult;
import com.hxc.common.utils.DateUtils;
import com.hxc.enums.DelTypes;
import com.hxc.enums.PutTypes;
import com.hxc.interest.domain.*;
import com.hxc.interest.mapper.*;
import com.hxc.interest.service.ITReactionBackService;
import com.hxc.interest.service.ITReactionDiscussService;
import com.hxc.interest.service.ITReactionInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反馈信息Service业务层处理
 * 
 * @author hxc
 * @date 2021-08-17
 */
@Service
public class TReactionInfoServiceImpl implements ITReactionInfoService 
{
    @Autowired
    private TReactionInfoMapper tReactionInfoMapper;

    @Autowired
    private TReactionUserMapper tReactionUserMapper;
    @Autowired
    private TCommunityInfoMapper tCommunityInfoMapper;
    @Autowired
    private TReactionThumbsupMapper tReactionThumbsupMapper;
    @Autowired
    private TReactionCareofMapper tReactionCareofMapper;

    @Autowired
    private ITReactionDiscussService tReactionDiscussService;
    @Autowired
    private ITReactionBackService tReactionBackService;

    /**
     * 查询反馈信息
     * 
     * @param tReactionInfo 反馈信息主键
     * @return 反馈信息
     */
    @Override
    public TReactionInfo selectTReactionInfo(TReactionInfo tReactionInfo)
    {
        TReactionInfo reactionInfo = tReactionInfoMapper.selectTReactionInfo(tReactionInfo);
        if(ObjectUtils.isNotEmpty(reactionInfo)){
            //判断上传图片是否为null
            if(StringUtils.isNotBlank(reactionInfo.getImgUrl())){
                reactionInfo.setImgList(reactionInfo.getImgUrl().split(","));
            }
        }
        return reactionInfo;
    }

    /**
     * 查询反馈信息列表
     * 
     * @param tReactionInfo 反馈信息
     * @return 反馈信息
     */
    @Override
    public List<TReactionInfo> selectTReactionInfoList(TReactionInfo tReactionInfo)
    {
        return tReactionInfoMapper.selectTReactionInfoList(tReactionInfo);
    }

    /**
     * 新增反馈信息
     * 
     * @param tReactionInfo 反馈信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTReactionInfo(TReactionInfo tReactionInfo)
    {
        //查询所在社区组织信息
        TCommunityInfo communityInfo = tCommunityInfoMapper.queryCommunityByVid(tReactionInfo.getVillagerId());
        if(ObjectUtils.isEmpty(communityInfo)){
            // 返回0为失败
            return 0;
        }
        //添加信息反馈表
        tReactionInfo.setCommunityId(communityInfo.getId());
        tReactionInfo.setCreateTime(DateUtils.getNowDate());
        int result = tReactionInfoMapper.insertTReactionInfo(tReactionInfo);
        //添加中间表
        TReactionUser reactionUser = new TReactionUser();
        reactionUser.setReactionId(tReactionInfo.getId());
        reactionUser.setUserId(tReactionInfo.getUserId());
        //点赞数 默认0
        reactionUser.setGreatNum(0);
        //删除状态默认0
        reactionUser.setDelFlag(DelTypes.NO.getCode());
        reactionUser.setSendType(PutTypes.ZIFA.getCode()); //自发
        reactionUser.setCreateTime(DateUtils.getNowDate());
        tReactionUserMapper.insertTReactionUser(reactionUser);
        //转交表操作
        //reactionCareo(communityInfo,tReactionInfo);
        return result;
    }

    //转交数据保存
    public void reactionCareo(TCommunityInfo communityInfo,TReactionInfo tReactionInfo){
        TReactionCareof tReactionCareof = new TReactionCareof();
        tReactionCareof.setCareofId(communityInfo.getVillagerId().intValue());//转交人id
        tReactionCareof.setReactionId(tReactionInfo.getId().intValue());//反馈事件id
        tReactionCareof.setCreateTime(new Date());//创建时间
        tReactionCareof.setCareStatus(0);//转交状态
        tReactionCareof.setIsView(0);//是否查看
        tReactionCareofMapper.insertTReactionCareof(tReactionCareof);
    }

    /**
     * 修改反馈信息
     * 
     * @param tReactionInfo 反馈信息
     * @return 结果
     */
    @Override
    public int updateTReactionInfo(TReactionInfo tReactionInfo)
    {
        return tReactionInfoMapper.updateTReactionInfo(tReactionInfo);
    }

    /**
     * 批量删除反馈信息
     * 
     * @param ids 需要删除的反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteTReactionInfoByIds(Long[] ids)
    {
        //批量删除(反馈与用户中间表)信息，主体数据不删除
        //tReactionInfoMapper.deleteTReactionInfoByIds(ids);
        return  tReactionUserMapper.deleteTReactionUserByIds(ids);
    }

    /**
     * 删除反馈信息信息
     * 
     * @param id 反馈信息主键
     * @return 结果
     */
    @Override
    public int deleteTReactionInfoById(Long id)
    {
        return tReactionInfoMapper.deleteTReactionInfoById(id);
    }

    /**
     * 功能描述: 根据id查询事件详情
     * @author yzj
     */
    @Override
    public AjaxResult queryReactionDetails(TReactionInfo tReactionInfo) {
        Map<String, Object> map = new HashMap<>();
        //获取事件详情信息
        TReactionInfo reactionInfo = selectTReactionInfo(tReactionInfo);
        if (ObjectUtils.isEmpty(reactionInfo)) {
            return AjaxResult.success("查询信息不存在");
        }
        map.put("reactionInfo", reactionInfo);
        //查询用户类列表评论
        TReactionDiscuss reactionDiscussOne = new TReactionDiscuss();
        reactionDiscussOne.setReauserId(tReactionInfo.getReauserId());
        List<TReactionDiscuss> reactionDiscussList = tReactionDiscussService.selectTReactionDiscussList(reactionDiscussOne);
        map.put("discussNum", reactionDiscussList.size());
        if(reactionDiscussList.size() > 0){
            reactionDiscussList.forEach(reactionDiscuss -> {
                //查询反馈评论列表信息
                TReactionDiscuss reactionDiscussTwo = new TReactionDiscuss();
                reactionDiscussTwo.setParentId(reactionDiscuss.getId());
                List<TReactionDiscuss> discussList = tReactionDiscussService.selectTReactionDiscussList(reactionDiscussTwo);
                reactionDiscuss.setReactionDiscussList(discussList);
            });
        }
        map.put("discussList", reactionDiscussList);
        //查询政务反馈信息
        TReactionBack reactionBack = new TReactionBack();
        reactionBack.setReactionId(reactionInfo.getId());
        List<TReactionBack> reactionBackList = tReactionBackService.selectTReactionBackList(reactionBack);
        map.put("backList", reactionBackList);
        return AjaxResult.success("success", map);
    }

    /**
     * 村民反馈事件列表(微信端)
     * @Author wsf
     * @Date 18:32 2021/8/18
     * @param tReactionInfo
     **/
    @Override
    public List<TReactionInfo> querySonReactionList(TReactionInfo tReactionInfo) {
        List<TReactionInfo> reactionList = tReactionInfoMapper.querySonReactionList(tReactionInfo);
        if(reactionList.size() > 0){
            reactionList.forEach(reactionInfo -> {
                //判断是否为民情反馈事件列表查询
                if (StringUtils.isBlank(tReactionInfo.getReactionStateEmus())){
                    TReactionThumbsup thumbsup = new TReactionThumbsup();
                    thumbsup.setReactionId(reactionInfo.getId());
                    thumbsup.setUserId(tReactionInfo.getUserId());
                    TReactionThumbsup thumbs = tReactionThumbsupMapper.selectTReactionThumbsup(thumbsup);
                    //判断该用户是否点赞
                    if (ObjectUtils.isNotEmpty(thumbs)){
                        reactionInfo.setThumbsup(true);
                    }
                }
                //判断上传图片是否为null
                if(StringUtils.isNotBlank(reactionInfo.getImgUrl())){
                    reactionInfo.setImgList(reactionInfo.getImgUrl().split(","));
                }
            });
        }
        return reactionList;
    }

    /**
     * 功能描述: 根据id查询信息
     * @author yzj
     * @param id
     */
    @Override
    public TReactionInfo selectTReactionInfoById(Long id) {
        TReactionInfo reactionInfo = tReactionInfoMapper.selectTReactionInfoById(id);
        if(ObjectUtils.isNotEmpty(reactionInfo)){
            //判断上传图片是否为null
            if(StringUtils.isNotBlank(reactionInfo.getImgUrl())){
                reactionInfo.setImgList(reactionInfo.getImgUrl().split(","));
            }
        }
        return reactionInfo;
    }

    /**
     * 村民反馈事件列表(钉钉端)
     * @Author wsf
     * @Date 18:32 2021/8/18
     * @param tReactionInfo
     **/
    @Override
    public List<TReactionInfo> queryHeadReactionList(TReactionInfo tReactionInfo) {
        List<TReactionInfo> reactionList = tReactionInfoMapper.queryHeadReactionList(tReactionInfo);
        if(reactionList.size() > 0){
            reactionList.forEach(reactionInfo -> {
                //判断上传图片是否为null
                if(StringUtils.isNotBlank(reactionInfo.getImgUrl())){
                    reactionInfo.setImgList(reactionInfo.getImgUrl().split(","));
                }
                //提交事件超出天数
                Long dayNum = DateUtil.between(reactionInfo.getCreateTime(), new Date(), DateUnit.MINUTE);
                reactionInfo.setDayNum(dayNum);
            });
        }
        return reactionList;
    }

    /**
     * 功能描述: 查询已评论事件信息
     * @author yzj
     */
    @Override
    public List<TReactionInfo> queryReactionListByDis(TReactionInfo tReactionInfo) {
        List<TReactionInfo> reactionList = tReactionInfoMapper.queryReactionListByDis(tReactionInfo);
        reactionList.forEach(reactionInfo -> {
            //判断上传图片是否为null
            if(StringUtils.isNotBlank(reactionInfo.getImgUrl())){
                reactionInfo.setImgList(reactionInfo.getImgUrl().split(","));
            }
        });
        return reactionList;
    }

}
